<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class TagsDB  extends DBReader{

    //根据tag_id 获取 tag 信息
    public function get($tag_id){

        $sql = 'select tag_id, tag_name from tags where tag_id = :tag_id';

        return $this->db()->getRow($sql,array('tag_id'=>$tag_id));
    }

    //根据tag_id 获取 tag 信息
    public function getTagInfoByName($tag_name){

        $sql = 'select tag_id, tag_name from tags where tag_name = :tag_name';

        return $this->db()->getRow($sql,array('tag_name'=>$tag_name));
    }

    //获取标签信息列表<带分页>
    public function getTagList($curr_page,$per_page,$filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['tag_name'])){
            $where .= ' and tag_name like :tag_name';
            $bindarr['tag_name'] = '%'.$filter['tag_name'].'%';
        }

        $ret = $this->pager($curr_page, $per_page)
            ->ct('select count(tag_id) from tags where '.$where,$bindarr)
            ->rs('select tag_id, tag_name from tags where '.$where.' order by tag_id desc', $bindarr);
        return $ret;
    }

    //获取所有标签信息
    public function getTags($filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['tag_name'])){
            $where .= ' and tag_name like :tag_name';
            $bindarr['tag_name'] = '%'.$filter['tag_name'].'%';
        }

        $sql = 'select tag_id, tag_name from tags where '. $where .' order by tag_id desc';

        $ret = $this->db()->getAll($sql,$bindarr);

        return $ret;
    }

    //添加标签信息
    public function addTag($tag_name){

        $sql = "insert into tags(tag_name) values(:tag_name)";

        $bindarr = array(
            'tag_name'=>$tag_name
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //删除标签信息
    public function delTag($tag_id){

        $sql = "delete from tags where tag_id = :tag_id";

        $bindarr = array(
            'tag_id'=>$tag_id,
        );
        $rs = $this->db()->execute($sql,$bindarr);

        return $rs->rowCount();
    }

    //更新标签信息
    public function updateTag($id,$name){

        $bindarr = array(
            'tag_id'=>$id,
            'tag_name'=>$name
        );
        $sql = "update tags set tag_name = :tag_name where tag_id = :tag_id";
        $rs = $this->db()->execute($sql,$bindarr);
        return $rs->rowCount();
    }
}